﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SmallERP.HostBll.BaseInfo;
using SmallERP.MODEL.FoundationModel;
using DevExpress.XtraEditors;
using System.Runtime.InteropServices;
using System.IO;
using SmallERP.COM;
using SmallERP.Host.Properties;

namespace SmallERP.Host.Frm
{
    public partial class FrmBaseInfo : Form
    {
        #region 私有变量

        private Printer_info _printerInfo = null;
        private PaperFace_Info _paperFaceInfo = null;

        private PaperType_Info _paperTypeInfo = null;
        private SysSetting_info _sysSetInfo = null;

        private GridSubArea_Info _subAreaInfo = null;

        private IDBAccess iDBAccess = null;
        List<BarSerInfo> _listR;
        #endregion
        public FrmBaseInfo()
        {
            InitializeComponent();
        }

        private void xtraTabControl_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            switch (e.Page.Name)
            {
                case "xtPagePrinter":
                    iDBAccess = new Printer();
                    iDBAccess.LinkGridControl(gcPrinter);
                    break;
                case "xtPagePaperFace":
                    iDBAccess = new Paperface();
                    iDBAccess.LinkGridControl(gcPaperFace);
                    break;
                case "xtPagePaperType":
                    iDBAccess = new Papertype();
                    iDBAccess.LinkGridControl(gcPaperType);
                    break;
                case "xtPageFilePath":
                    GetFilePath();
                    break;
                case "xtPageSubArea":
                    iDBAccess = new Subarea();
                    iDBAccess.LinkGridControl(gcSubArea);
                    break;
                case "xtraTabPageMakeBig":
                    InitMakeBagPic();
                    break;
                case "xtraTabPageOther":
                    InitOther();
                    break;


            }
        }

        private void InitOther()
        {
            IniFile.FilePath = Application.StartupPath;
            string RemarkKeysColor = IniFile.IniReadValue("Host", "RemarkKeysColor");
            DateTransfer.RemarkKeysColor = RemarkKeysColor;
            int iCount = 1;
            foreach (string strKV in RemarkKeysColor.Split('#')) 
            {
                if (string.IsNullOrEmpty(strKV))
                {
                    continue;
                }
                string[] keysValue = strKV.Split('@');
                int R=Convert.ToInt16(keysValue[1].Split(',')[0]);
                int G=Convert.ToInt16(keysValue[1].Split(',')[1]);
                int B=Convert.ToInt16(keysValue[1].Split(',')[2]);
                switch(iCount)
                {
                    case 1:
                   
                    textBoxkey1.Text = keysValue[0];
                    labelRemarkColor1.BackColor = Color.FromArgb(R, G, B);
                    break;
                    case 2:
                    textBoxkey2.Text = keysValue[0];
                    labelRemarkColor2.BackColor = Color.FromArgb(R, G, B);
                    break;
                    case 3:
                    textBoxkey3.Text = keysValue[0];
                    labelRemarkColor3.BackColor = Color.FromArgb(R, G, B);
                    break;
                    case 4:
                    textBoxkey4.Text = keysValue[0];
                    labelRemarkColor4.BackColor = Color.FromArgb(R, G, B);
                    break;
                    case 5:
                    textBoxkey5.Text = keysValue[0];
                    labelRemarkColor5.BackColor = Color.FromArgb(R, G, B);
                    break;
                    case 6:
                    textBoxkey6.Text = keysValue[0];
                    labelRemarkColor6.BackColor = Color.FromArgb(R, G, B);
                    break;
                    case 7:
                    textBoxkey7.Text = keysValue[0];
                    labelRemarkColor7.BackColor = Color.FromArgb(R, G, B);
                    break;
                    case 8:
                    textBoxkey8.Text = keysValue[0];
                    labelRemarkColor8.BackColor = Color.FromArgb(R, G, B);
                    break;

                }
                iCount++;

            }

        }

        public void InitMakeBagPic()
        {
            
            IniFile.FilePath = Application.StartupPath;
            spinEdit1.Text = IniFile.IniReadValue("Host", "FontSize");

            barCodeSize.Text = IniFile.IniReadValue("Host", "BarCodeSize");

            //checkBox2.Checked = Convert.ToBoolean(IniFileOper.IniReadValue("Host", "BarCodeFontIsBold"));
            string AddIccByPsStr = IniFile.IniReadValue("Host", "AddIccByPs");
            if (string.IsNullOrEmpty(AddIccByPsStr))
            {
                IniFile.IniWriteValue("Host", "AddIccByPs", "0");
                comboBoxICC.SelectedIndex = Convert.ToInt16(IniFile.IniReadValue("Host", "AddIccByPs"));

            }
            else
            {
                comboBoxICC.SelectedIndex = Convert.ToInt16(AddIccByPsStr);
 
            }
            string CodeHeightStr = IniFile.IniReadValue("Host", "CodeHeight");
            if (string.IsNullOrEmpty(CodeHeightStr))
            {
                IniFile.IniWriteValue("Host", "CodeHeight", "25");

                spinEditCodeHeight.Value = Convert.ToInt16(IniFile.IniReadValue("Host", "CodeHeight"));

            }
            else
            {
                spinEditCodeHeight.Value = Convert.ToInt16(CodeHeightStr);
            }
            string IsPrintBoardInfoStr = IniFile.IniReadValue("Host", "IsPrintBoardInfo");
            if (string.IsNullOrEmpty(IsPrintBoardInfoStr))
            {
                IniFile.IniWriteValue("Host", "IsPrintBoardInfo", "False");

                checkBoxIsBoardInfo.Checked = Convert.ToBoolean(IniFile.IniReadValue("Host", "IsPrintBoardInfo"));

            }
            else
            {
                checkBoxIsBoardInfo.Checked = Convert.ToBoolean(IsPrintBoardInfoStr);
            }

            string IsPrintLineStr = IniFile.IniReadValue("Host", "IsPrintLine");
            if (string.IsNullOrEmpty(IsPrintLineStr))
            {
                IniFile.IniWriteValue("Host", "IsPrintLine", "True");

                checkBoxIsprintLine.Checked = Convert.ToBoolean(IniFile.IniReadValue("Host", "IsPrintLine"));

            }
            else
            {
                checkBoxIsprintLine.Checked = Convert.ToBoolean(IsPrintLineStr);
 
            }
            string IsAutoCutStr = IniFile.IniReadValue("Host", "IsAutoCut");
            if (string.IsNullOrEmpty(IsAutoCutStr))
            {
                IniFile.IniWriteValue("Host", "IsAutoCut", "False");

                checkBoxAutocut.Checked = Convert.ToBoolean(IniFile.IniReadValue("Host", "IsAutoCut"));

            }
            else
            {
                checkBoxAutocut.Checked = Convert.ToBoolean(IsAutoCutStr);
            }
            string HistoryDataSaveDaysStr = IniFile.IniReadValue("Host", "HistoryDataSaveDays");
            if (string.IsNullOrEmpty(HistoryDataSaveDaysStr))
            {
                IniFile.IniWriteValue("Host", "HistoryDataSaveDays", "5");

                txtDays.Text = IniFile.IniReadValue("Host", "HistoryDataSaveDays");

            }
            else
            {
                txtDays.Text = HistoryDataSaveDaysStr;
            }

            string BarCodeTypeStr = IniFile.IniReadValue("Host", "BarCodeType");
            if (string.IsNullOrEmpty(BarCodeTypeStr))
            {
                IniFile.IniWriteValue("Host", "BarCodeType", "1");
                comboBoxBarCodeType.SelectedIndex = Convert.ToInt16(IniFile.IniReadValue("Host", "BarCodeType"));

            }
            else
            {
                comboBoxBarCodeType.SelectedIndex = Convert.ToInt16(BarCodeTypeStr);
            }

            string MinPhotoRemarkLengthStr = IniFile.IniReadValue("Host", "MinPhotoRemarkLength");
            if (string.IsNullOrEmpty(MinPhotoRemarkLengthStr))
            {
                IniFile.IniWriteValue("Host", "MinPhotoRemarkLength", "3");

                textEditMinbarLength.Text = IniFile.IniReadValue("Host", "MinPhotoRemarkLength");

            }
            else
            {
                textEditMinbarLength.Text = MinPhotoRemarkLengthStr;
            }
            string ColorString = IniFile.IniReadValue("Host", "BackColor");
            if (string.IsNullOrEmpty(ColorString))
            {
                label10.BackColor = Color.Gray;

            }
            else
            {
                string[] colorArray = ColorString.Split(',');
                label10.BackColor = Color.FromArgb(Convert.ToInt32(colorArray[0]), Convert.ToInt32(colorArray[1]), Convert.ToInt32(colorArray[2]));
            }
            string LineColorString = IniFile.IniReadValue("Host", "LineBackColor");
            if (string.IsNullOrEmpty(LineColorString))
            {
                labelLineColor.BackColor = Color.Black;
                IniFile.IniWriteValue("Host", "LineBackColor", Color.Black.R + "," + Color.Black.G + "," + Color.Black.B);
              
            }
            else
            {
                string[] colorArray = LineColorString.Split(',');
                labelLineColor.BackColor = Color.FromArgb(Convert.ToInt32(colorArray[0]), Convert.ToInt32(colorArray[1]), Convert.ToInt32(colorArray[2]));
            }
            string CutMinPhotoNumStr = IniFile.IniReadValue("Host", "CutMinPhotoNum");
            if (string.IsNullOrEmpty(CutMinPhotoNumStr))
            {
                IniFile.IniWriteValue("Host", "CutMinPhotoNum", "2");

                textEditCutMinPhotoNum.Text = IniFile.IniReadValue("Host", "CutMinPhotoNum");

            }
            else
            {
                textEditCutMinPhotoNum.Text = CutMinPhotoNumStr;
            }
            string PhotoRemarkFlagStr = IniFile.IniReadValue("Host", "PhotoRemarkFlag");
            if (string.IsNullOrEmpty(PhotoRemarkFlagStr))
            {
                IniFile.IniWriteValue("Host", "PhotoRemarkFlag", "0");

                comboBoxPhotoRemarkFlag.SelectedIndex = Convert.ToInt16(IniFile.IniReadValue("Host", "PhotoRemarkFlag"));

            }
            else
            {
                comboBoxPhotoRemarkFlag.SelectedIndex = Convert.ToInt16(PhotoRemarkFlagStr);
            }
            string TextAlignTypeStr = IniFile.IniReadValue("Host", "TextAlignType");
            if (string.IsNullOrEmpty(TextAlignTypeStr))
            {
                IniFile.IniWriteValue("Host", "TextAlignType", "0");

                comboBoxAlignType.SelectedIndex = Convert.ToInt16(IniFile.IniReadValue("Host", "TextAlignType"));

            }
            else
            {
                comboBoxAlignType.SelectedIndex = Convert.ToInt16(TextAlignTypeStr);
            }
            string SaveFileTypeStr = IniFile.IniReadValue("Host", "SaveFileType");
            if (string.IsNullOrEmpty(SaveFileTypeStr))
            {
                IniFile.IniWriteValue("Host", "SaveFileType", "0");
                comboBoxSaveColorType.SelectedIndex = Convert.ToInt16(IniFile.IniReadValue("Host", "SaveFileType"));

            }
            else
            {
                comboBoxSaveColorType.SelectedIndex = Convert.ToInt16(SaveFileTypeStr);
 
            }
            string TextFontSizeStr = IniFile.IniReadValue("Host", "TextFontSize");
            if (string.IsNullOrEmpty(TextFontSizeStr))
            {
                IniFile.IniWriteValue("Host", "TextFontSize", "8");
                spinEdit1.Text = IniFile.IniReadValue("Host", "TextFontSize");
            }
            else
            {
                spinEdit1.Text = TextFontSizeStr;
            }

            string TextFontStr = IniFile.IniReadValue("Host", "TextFont");
            if (string.IsNullOrEmpty(TextFontStr))
            {
                IniFile.IniWriteValue("Host", "TextFont", "Arial");
                comboBoxFont.Text = IniFile.IniReadValue("Host", "TextFont");
            }
            else
            {
                comboBoxFont.Text = TextFontStr;
            }
            System.Drawing.Text.InstalledFontCollection fontsn = new System.Drawing.Text.InstalledFontCollection();
            foreach (System.Drawing.FontFamily ff in fontsn.Families)
            {
                this.comboBoxFont.Items.Add(ff.Name);
            }
            if (!string.IsNullOrEmpty(IniFile.IniReadValue("Host", "TextFont")))
            {
                comboBoxFont.Text = IniFile.IniReadValue("Host", "TextFont");
            }
            else
            {
                comboBoxFont.Text = fontsn.Families.FirstOrDefault().Name;
            }
            string PhotosizeUnit = IniFile.IniReadValue("Host", "PhotosizeUnit");
            if (string.IsNullOrEmpty(PhotosizeUnit))
            {

                IniFile.IniWriteValue("Host", "PhotosizeUnit", "0");
                //DateTransfer.PhotosizeUnit = Convert.ToInt32(IniFileOper.IniReadValue("Host", "PhotosizeUnit"));
                comboBoxPhotosizeUnit.SelectedIndex = 0;
            }
            else
            {
                comboBoxPhotosizeUnit.SelectedIndex = Convert.ToInt32(IniFile.IniReadValue("Host", "PhotosizeUnit"));
            }

            string CutInfoPosition = IniFile.IniReadValue("Host", "CutInfoPosition");
            if (string.IsNullOrEmpty(CutInfoPosition))
            {

                IniFile.IniWriteValue("Host", "CutInfoPosition", "0");
                //DateTransfer.PhotosizeUnit = Convert.ToInt32(IniFileOper.IniReadValue("Host", "PhotosizeUnit"));
                comboBoxCutInfoPosition.SelectedIndex = 0;
            }
            else
            {
                comboBoxCutInfoPosition.SelectedIndex = Convert.ToInt32(IniFile.IniReadValue("Host", "CutInfoPosition"));
            }

            string strRemarkInPicBottomOrTop = IniFile.IniReadValue("Host", "RemarkInPicBottomOrTop");
            if (string.IsNullOrEmpty(strRemarkInPicBottomOrTop))
            {
                IniFile.IniWriteValue("Host", "RemarkInPicBottomOrTop", "0");
                comboBoxBottomOrTop.SelectedIndex = 0;
            }
            else
            {
                comboBoxBottomOrTop.SelectedIndex = Convert.ToInt32(strRemarkInPicBottomOrTop);
            }
            string strRemarkInPicRightOrLeft = IniFile.IniReadValue("Host", "RemarkInPicRightOrLeft");
            if (string.IsNullOrEmpty(strRemarkInPicRightOrLeft))
            {
                IniFile.IniWriteValue("Host", "RemarkInPicRightOrLeft", "0");
                comboBoxBottomOrTop.SelectedIndex = 0;
            }
            else
            {
                comboBoxRightOrLeft.SelectedIndex = Convert.ToInt32(strRemarkInPicRightOrLeft);
            }
            checkBoxTwoFlag.Checked = DateTransfer.TwoCutFlag;

            textEditMergenMM.Text=DateTransfer.MerginMM.ToString();
            checkBoxCutNobarCode.Checked = DateTransfer.CutNobarCode;

            Bleed bleed = DateTransfer.CurrentBleed;
            comboBoxBleed.SelectedIndex = bleed.Type;
            textEditMergenMM.Text = bleed.Value.ToString();
            if (comboBoxBleed.SelectedIndex == 0)
            {
                labelBleedUnit.Text = "mm";
            }
            else
            {
                labelBleedUnit.Text = "%";
            }
            _listR = GetInitil();
            gridControl1.DataSource = _listR;
        }
        private void Frmmain_Load(object sender, EventArgs e)
        {
            //绑定打印机
            iDBAccess = new Printer();
            iDBAccess.LinkGridControl(gcPrinter);

            //绑定纸型
            LinkPaperType();
        }
        public static List<BarSerInfo> GetInitil()
        {
            List<BarSerInfo> listR = new List<BarSerInfo>();
            string barSer = IniFile.IniReadValue("Host", "BarSetting");
            if (string.IsNullOrEmpty(barSer))
            {
                barSer = "1_F1_文件夹1_1,2_F2_文件夹2_1,3_PN_产品名称_1,4_PNAME_照片名_1,5_PREMARK_照片路径_1,6_PPN_制作名称_1,7_ONUM_产品号_1,8_PNUM_照片编号_1,9_PSIZE_尺寸信息_1,10_DATE_打印时间_1,11_PHOTOORDER_文件包照片数_1,12_BID_版号_1";
                Settings.Default.BarSetting = barSer;
                Settings.Default.Save();

            }
            foreach (var barSet in barSer.Split(new char[] { ',' }))
            {
                //index_code_des_isprint
                var set = barSet.Split(new char[] { '_' });
                listR.Add(new BarSerInfo()
                {
                    BarIndex = Int32.Parse(set[0]),
                    Code = set[1],
                    Description = set[2],
                    IsPrint = set[3] == "1" ? true : false
                });
            }
            listR = listR.OrderBy(s => s.BarIndex).ToList();

            return listR;
        }

        #region 打印机


        //添加打印机
        private void btnAddPrinter_Click(object sender, EventArgs e)
        {
            iDBAccess = new Printer();
            Cursor = Cursors.WaitCursor;
            btnAddPrinter.Enabled = false;
            if (DataCheck())
            {
                Printer_info printerInfo = new Printer_info();
                printerInfo.HotDirPath = txtHotFilePath.Text.Trim();
                printerInfo.IsTransverse = chkIsHorizontalPrint.Checked;
                printerInfo.MonitorDirPath = txtMonitorFilePath.Text.Trim();
                printerInfo.PBstatus = 0;
                printerInfo.PDes = txtPrinterDes.Text.Trim();
                printerInfo.PDPI = Convert.ToInt32(txtDPI.Text.Trim());
                printerInfo.PLength = Convert.ToDouble(txtPrinterLength.Text.Trim());
                printerInfo.PName = txtPrinterName.Text.Trim();
                printerInfo.PrintStatus = 0;
                printerInfo.PWidth = Convert.ToDouble(txtPrinterWidth.Text.Trim());
                printerInfo.HotFolderUserName = textEditU1.Text.Trim();
                printerInfo.HotFolderPwd = textEditP1.Text.Trim();
                printerInfo.IsMiddle = checkEditIsMiddle.Checked;
                printerInfo.PrinterType = comboBoxPrinterType.SelectedIndex;
                printerInfo.IsHorizontal = checkEditIsHorizontal.Checked;
                int ret = iDBAccess.Add(printerInfo);
                if (ret == -1)
                {
                    MessageBox.Show("添加失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 1)
                {
                    iDBAccess.LinkGridControl(gcPrinter);
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("该打印机在数据库中已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnAddPrinter.Enabled = true;
        }
        //更新打印机
        private void btnUpdatePrinter_Click(object sender, EventArgs e)
        {
            iDBAccess = new Printer();
            Cursor = Cursors.WaitCursor;
            btnUpdatePrinter.Enabled = false;
            if (DataCheck())
            {
                Printer_info printerInfo = new Printer_info();
                printerInfo.PrinterID = _printerInfo.PrinterID;
                printerInfo.HotDirPath = txtHotFilePath.Text.Trim();
                printerInfo.IsTransverse = chkIsHorizontalPrint.Checked;
                printerInfo.MonitorDirPath = txtMonitorFilePath.Text.Trim();
                printerInfo.PBstatus = 0;
                printerInfo.PDes = txtPrinterDes.Text.Trim();
                printerInfo.PDPI = Convert.ToInt32(txtDPI.Text.Trim());
                printerInfo.PLength = Convert.ToDouble(txtPrinterLength.Text.Trim());
                printerInfo.PName = txtPrinterName.Text.Trim();
                printerInfo.PrintStatus = 0;
                printerInfo.PWidth = Convert.ToDouble(txtPrinterWidth.Text.Trim());
                printerInfo.HotFolderUserName = textEditU1.Text.Trim();
                printerInfo.HotFolderPwd = textEditP1.Text.Trim();
                printerInfo.IsMiddle = checkEditIsMiddle.Checked;
                printerInfo.PrinterType = comboBoxPrinterType.SelectedIndex;
                printerInfo.IsHorizontal = checkEditIsHorizontal.Checked;
                int ret = iDBAccess.Update(printerInfo);
                if (ret == -1)
                {
                    MessageBox.Show("更新失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 1)
                {
                    iDBAccess.LinkGridControl(gcPrinter);
                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("数据库中已经存在一个该名称的打印机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnUpdatePrinter.Enabled = true;

        }

        //删除打印机
        private void btnDeletePrinter_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("是否要删除该打印机及分配给该打印机的所有的版和打印纸信息？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                iDBAccess = new Printer();
                Cursor = Cursors.WaitCursor;
                btnDeletePrinter.Enabled = false;
                if (_printerInfo != null)
                {
                    int ret = iDBAccess.Delete(_printerInfo);
                    if (ret == -1)
                    {
                        MessageBox.Show("删除失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        iDBAccess.LinkGridControl(gcPrinter);
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("请选择要删除的打印机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                Cursor = Cursors.Default;
                btnDeletePrinter.Enabled = true;
            }
        }
        private bool DataCheck()
        {


            if (IsTextNull(txtPrinterName, "打印机名称"))
            {
                return false;
            }
            if (IsTextNull(txtPrinterDes, "打印机描述"))
            {
                return false;
            }
            if (IsTextNull(txtPrinterLength, "打印纸长"))
            {
                return false;
            }
            if (IsTextNull(txtPrinterWidth, "打印纸宽"))
            {
                return false;
            }
            if (IsTextNull(txtHotFilePath, "热文件夹路径"))
            {
                return false;
            }
            if (IsTextNull(txtMonitorFilePath, "监控文件夹路径"))
            {
                return false;
            }
            if (IsTextNull(txtDPI, "打印机DPI"))
            {
                return false;
            }
            return true;

        }
        /// <summary>
        /// 判断文本框是否为空
        /// </summary>
        /// <param name="text"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private bool IsTextNull(TextEdit text, string name)
        {
            if (string.IsNullOrEmpty(text.Text.Trim()))
            {
                MessageBox.Show("请输入" + name, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                text.Focus();
                return true;
            }
            return false;
        }
        //焦点行改变事件
        private void gvPrinter_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _printerInfo = (Printer_info)gvPrinter.GetFocusedRow();
            if (_printerInfo != null)
            {
                txtDPI.Text = _printerInfo.PDPI.ToString();
                txtHotFilePath.Text = _printerInfo.HotDirPath;
                txtMonitorFilePath.Text = _printerInfo.MonitorDirPath;
                txtPrinterDes.Text = _printerInfo.PDes;
                txtPrinterLength.Text = _printerInfo.PLength.ToString();
                txtPrinterName.Text = _printerInfo.PName;
                txtPrinterWidth.Text = _printerInfo.PWidth.ToString();
                chkIsHorizontalPrint.Checked = _printerInfo.IsTransverse;
                textEditU1.Text = _printerInfo.HotFolderUserName;
                textEditP1.Text = _printerInfo.HotFolderPwd;
                checkEditIsMiddle.Checked = _printerInfo.IsMiddle;
                comboBoxPrinterType.SelectedIndex = _printerInfo.PrinterType;
                checkEditIsHorizontal.Checked = _printerInfo.IsHorizontal;
            }
            cmbPrinterPaperType.Text = "";
            txtSharpValue.Text = "";
            txtICCFilePath.Text = "";
        }
        //选择监控文件夹
        private void btnMonitorFileBrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog.ShowDialog())
            {
                string path = folderBrowserDialog.SelectedPath;
                if (!path.Contains(@"\\"))
                {
                    MessageBox.Show("请选择网络路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                txtMonitorFilePath.Text = path;

            }

        }
        //选择热文件夹
        private void btnHotFileBrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog.ShowDialog())
            {
                string path = folderBrowserDialog.SelectedPath;
                if (!path.Contains(@"\\"))
                {
                    MessageBox.Show("请选择网络路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                txtHotFilePath.Text = path;
            }
        }

        //选择ICC文件
        private void btnICCFileBrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == openFileDialog.ShowDialog())
            {
                txtICCFilePath.Text = openFileDialog.FileName;
            }
        }
        //设置打印机ICC路径及锐化值
        private void btnICCOK_Click(object sender, EventArgs e)
        {

            PrinterSet printerSet = new PrinterSet();
            Papertype papertype = new Papertype();
            if (string.IsNullOrEmpty(cmbPrinterPaperType.Text.Trim()))
            {
                MessageBox.Show("请选择纸型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty(txtSharpValue.Text.Trim()))
            {
                MessageBox.Show("请输入锐化值", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSharpValue.Focus();
                return;
            }
            PrinterSeting_info printerSetInfo = printerSet.GetPrinterIccPath(_printerInfo.PrinterID, cmbPrinterPaperType.Text);
            if (printerSetInfo != null)
            {
                printerSetInfo.ICCPATH = txtICCFilePath.Text;
                printerSetInfo.Sharpening = Convert.ToInt32(txtSharpValue.Text);

                int ret = printerSet.Update(printerSetInfo);
                if (ret == 1)
                {
                    MessageBox.Show("设置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("设置失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                printerSetInfo = new PrinterSeting_info();
                printerSetInfo.PrinterId = _printerInfo.PrinterID;
                printerSetInfo.PaperTypeId = papertype.GetPaperType(cmbPrinterPaperType.Text).PaperTypeId;
                printerSetInfo.ICCPATH = txtICCFilePath.Text;
                printerSetInfo.Sharpening = Convert.ToInt32(txtSharpValue.Text);
                int ret = printerSet.Insert(printerSetInfo);
                if (ret == 1)
                {
                    MessageBox.Show("设置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("设置失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        /// <summary>
        /// 绑定纸型
        /// </summary>
        private void LinkPaperType()
        {
            Papertype paperType = new Papertype();
            cmbPrinterPaperType.Properties.Items.Clear();
            foreach (var item in paperType.GetPaperTypeList())
            {
                cmbPrinterPaperType.Properties.Items.Add(item.PaperTypeName);
            }
        }

        private void cmbPrinterPaperType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //PrinterSet printerSet = new PrinterSet();
            //PrinterSeting_info printerSetInfo = printerSet.GetPrinterIccPath(_printerInfo.PrinterID, cmbPrinterPaperType.Text);
            //if (printerSetInfo != null)
            //{
            //    txtSharpValue.Text = printerSetInfo.Sharpening.ToString();
            //    txtICCFilePath.Text = printerSetInfo.ICCPATH;
            //}
            //else
            //{
            //    txtSharpValue.Text = string.Empty;
            //    txtICCFilePath.Text = string.Empty;
            //}
        }
        #endregion

        #region 纸面

        private void gvPaperFace_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _paperFaceInfo = (PaperFace_Info)gvPaperFace.GetFocusedRow();
            if (_paperFaceInfo != null)
            {
                txtPaperFace.Text = _paperFaceInfo.PaperFaceName;
            }

        }

        //添加纸面
        private void btnAddPaperFace_Click(object sender, EventArgs e)
        {
            iDBAccess = new Paperface();
            Cursor = Cursors.WaitCursor;
            btnAddPaperFace.Enabled = false;
            if (!IsPaperFaceTextNull())
            {

                PaperFace_Info paperFaceInfo = new PaperFace_Info();
                paperFaceInfo.PaperFaceName = txtPaperFace.Text;
                int ret = iDBAccess.Add(paperFaceInfo);
                if (ret == -1)
                {
                    MessageBox.Show("添加失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret > 0)
                {
                    iDBAccess.LinkGridControl(gcPaperFace);
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("该纸面在数据库中已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnAddPaperFace.Enabled = true;
        }

        private bool IsPaperFaceTextNull()
        {
            if (string.IsNullOrEmpty(txtPaperFace.Text.Trim()))
            {
                MessageBox.Show("请输入纸面名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPaperFace.Focus();
                return true;
            }

            return false;
        }
        //更新纸面
        private void btnUpdatePaperFace_Click(object sender, EventArgs e)
        {
            iDBAccess = new Paperface();
            Cursor = Cursors.WaitCursor;
            btnUpdatePaperFace.Enabled = false;
            if (!IsPaperFaceTextNull())
            {

                PaperFace_Info paperFaceInfo = new PaperFace_Info();
                paperFaceInfo.PaperFaceName = txtPaperFace.Text;
                paperFaceInfo.PaperFaceId = _paperFaceInfo.PaperFaceId;
                int ret = iDBAccess.Update(paperFaceInfo);
                if (ret == -2)
                {
                    MessageBox.Show("更新失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret > 0)
                {
                    iDBAccess.LinkGridControl(gcPaperFace);
                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnUpdatePaperFace.Enabled = true;

        }
        //删除纸面
        private void btnDeletePaperFace_Click(object sender, EventArgs e)
        {
            iDBAccess = new Paperface();
            Cursor = Cursors.WaitCursor;
            btnDeletePaperFace.Enabled = false;
            if (_paperFaceInfo != null)
            {
                int ret = iDBAccess.Delete(_paperFaceInfo);
                if (ret == -1)
                {
                    MessageBox.Show("删除失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    iDBAccess.LinkGridControl(gcPaperFace);
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的纸面", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            Cursor = Cursors.Default;
            btnDeletePaperFace.Enabled = true;

        }

        #endregion

        #region 纸型

        private void gvPaperType_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _paperTypeInfo = (PaperType_Info)gvPaperType.GetFocusedRow();
            if (_paperTypeInfo != null)
            {
                txtPaperType.Text = _paperTypeInfo.PaperTypeName;
            }
        }

        //添加纸型
        private void btnPaperTypeAdd_Click(object sender, EventArgs e)
        {
            iDBAccess = new Papertype();
            Cursor = Cursors.WaitCursor;
            btnPaperTypeAdd.Enabled = false;
            if (!IsPaperTypeTextNull())
            {

                PaperType_Info paperTypeInfo = new PaperType_Info();
                paperTypeInfo.PaperTypeName = txtPaperType.Text;
                int ret = iDBAccess.Add(paperTypeInfo);
                if (ret == -1)
                {
                    MessageBox.Show("添加失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 1)
                {
                    iDBAccess.LinkGridControl(gcPaperType);
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("该纸型在数据库中已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnPaperTypeAdd.Enabled = true;

        }
        private bool IsPaperTypeTextNull()
        {
            if (string.IsNullOrEmpty(txtPaperType.Text.Trim()))
            {
                MessageBox.Show("请输入纸型名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPaperType.Focus();
                return true;
            }

            return false;

        }
        //更新纸型
        private void btnPaperTypeUpdate_Click(object sender, EventArgs e)
        {
            iDBAccess = new Papertype();
            Cursor = Cursors.WaitCursor;
            btnPaperTypeUpdate.Enabled = false;
            if (!IsPaperTypeTextNull())
            {

                PaperType_Info paperTypeInfo = new PaperType_Info();
                paperTypeInfo.PaperTypeId = _paperTypeInfo.PaperTypeId;
                paperTypeInfo.PaperTypeName = txtPaperType.Text;
                int ret = iDBAccess.Update(paperTypeInfo);
                if (ret == -1)
                {
                    MessageBox.Show("更新失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 1)
                {
                    iDBAccess.LinkGridControl(gcPaperType);
                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("纸型未找到", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnPaperTypeUpdate.Enabled = true;

        }
        //删除纸型
        private void btnPaperTypeDelete_Click(object sender, EventArgs e)
        {
            iDBAccess = new Papertype();
            Cursor = Cursors.WaitCursor;
            btnPaperTypeDelete.Enabled = false;
            if (!IsPaperTypeTextNull())
            {
                if (_paperTypeInfo != null)
                {

                    int ret = iDBAccess.Delete(_paperTypeInfo);
                    if (ret == -1)
                    {
                        MessageBox.Show("删除失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        iDBAccess.LinkGridControl(gcPaperType);
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            Cursor = Cursors.Default;
            btnPaperTypeDelete.Enabled = true;

        }

        #endregion

        #region 文件路径

        private void GetFilePath()
        {
            SysSet sysSet = new SysSet();
            _sysSetInfo = sysSet.GetSysSet();
            if (_sysSetInfo != null)
            {
                txtBigPicPath.Text = _sysSetInfo.BigPicSavePath;
                txtOrderPath.Text = _sysSetInfo.SysOrderPath;
                textEditU.Text = _sysSetInfo.BigUserName;
                textEditP.Text = _sysSetInfo.BigPWD;
                if (_sysSetInfo.SysOrderPath.Equals("delete"))
                {
                    radioButtonDelete.Checked = true;
                    txtOrderPath.Text = "";
                    btnOrderPathBrowser.Enabled = false;
                    txtOrderPath.Enabled = false;
                }
                else if (_sysSetInfo.SysOrderPath.Equals("keep"))
                {

                    radioButtonKeep.Checked = true;
                    txtOrderPath.Text = "";
                    btnOrderPathBrowser.Enabled = false;
                    txtOrderPath.Enabled = false;
                }
                else
                {
                    radioButtonDelive.Checked = true;
                    txtOrderPath.Text = _sysSetInfo.SysOrderPath;
                    btnOrderPathBrowser.Enabled = true; ;
                }
            }
            IniFile.FilePath = Application.StartupPath;
            string BoolFilePrinterName = IniFile.IniReadValue("Host", "BoolFilePrinterName");

            if (string.IsNullOrEmpty(BoolFilePrinterName))
            {
                IniFile.IniWriteValue("Host", "BoolFilePrinterName", "false");

                checkPrinterName.Checked = false;

            }
            else
            {
                checkPrinterName.Checked = Convert.ToBoolean(BoolFilePrinterName);
            }

            string BoolTempleteName = IniFile.IniReadValue("Host", "BoolTempleteName");

            if (string.IsNullOrEmpty(BoolTempleteName))
            {
                IniFile.IniWriteValue("Host", "BoolTempleteName", "false");

                checkSize.Checked = false;

            }
            else
            {
                checkSize.Checked = Convert.ToBoolean(BoolTempleteName);
            }
            string CutInfoPath = IniFile.IniReadValue("Host", "CutInfo");
            textEditCutInfoPath.Text = CutInfoPath;
        }

        private void btnBigPicPathBrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog.ShowDialog())
            {
                string path = folderBrowserDialog.SelectedPath;
                ////if (!path.Contains(@"\\"))
                ////{
                //    MessageBox.Show("请选择网络路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                ////}
                txtBigPicPath.Text = path;
            }
        }

        private void btnOrderPathBrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog.ShowDialog())
            {
                string path = folderBrowserDialog.SelectedPath;
                //if (!path.Contains(@"\\"))
                //{
                //    MessageBox.Show("请选择网络路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}
                txtOrderPath.Text = path;
            }

        }

        private void btnFilePathUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtBigPicPath.Text.Trim()))
            {
                MessageBox.Show("请选择生成大图文件夹路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (radioButtonDelive.Checked && string.IsNullOrEmpty(txtOrderPath.Text.Trim()))
            {
                MessageBox.Show("请选择订单文件夹路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            SysSet sysSet = new SysSet();

            SysSetting_info sysSetInfo = new SysSetting_info();
            sysSetInfo.BigPicSavePath = txtBigPicPath.Text;
            sysSetInfo.SysOrderPath = txtOrderPath.Text;
            sysSetInfo.BigUserName = textEditU.Text;
            sysSetInfo.BigPWD = textEditP.Text;
            if (radioButtonKeep.Checked)
            {
                sysSetInfo.SysOrderPath = "keep";
            }
            if (radioButtonDelive.Checked)
            {
                sysSetInfo.SysOrderPath = txtOrderPath.Text;
            }
            if (radioButtonDelete.Checked)
            {
                sysSetInfo.SysOrderPath = "delete";
            }

            IniFile.FilePath = Application.StartupPath;
            string cutPath=textEditCutInfoPath.Text.Trim();
           
            if (Directory.Exists(cutPath))
            {
                IniFile.IniWriteValue("Host", "CutInfo", cutPath.TrimEnd('\\'));
            }
            else
            {
                IniFile.IniWriteValue("Host", "CutInfo", "c:");
                MessageBox.Show("设定目录无法访问，已经修改为c:！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 
            }
            
            int ret = sysSet.Update(sysSetInfo);
            if (ret == 1)
            {
                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("更新失败，请稍候再试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion



        #region 格子分区

        private void gvSubArea_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _subAreaInfo = (GridSubArea_Info)gvSubArea.GetFocusedRow();
            if (_subAreaInfo != null)
            {
                txtHeardTitle.Text = _subAreaInfo.NameHeadChar;
                txtHeightNum.Text = _subAreaInfo.HeightNum.ToString();
                txtOutTimeAlarm.Text = _subAreaInfo.OutTimeAlarmTime.ToString();
                txtSubAreaName.Text = _subAreaInfo.SubAreaName;
                txtWidthNum.Text = _subAreaInfo.WidthNum.ToString();
            }
        }
        //添加分区
        private void btnSubAreaAdd_Click(object sender, EventArgs e)
        {
            iDBAccess = new Subarea();
            Cursor = Cursors.WaitCursor;
            btnSubAreaAdd.Enabled = false;
            if (SubAreaDataCheck())
            {

                GridSubArea_Info subAreaInfo = new GridSubArea_Info();
                subAreaInfo.HeightNum = Convert.ToInt32(txtHeightNum.Text);
                subAreaInfo.IsUseful = chkSubAreaIsUse.Checked;
                subAreaInfo.NameHeadChar = txtHeardTitle.Text;
                subAreaInfo.OutTimeAlarmTime = Convert.ToInt32(txtOutTimeAlarm.Text);
                subAreaInfo.SubAreaName = txtSubAreaName.Text;
                subAreaInfo.WidthNum = Convert.ToInt32(txtWidthNum.Text);

                int ret = iDBAccess.Add(subAreaInfo);
                if (ret == -1)
                {
                    MessageBox.Show("添加失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 1)
                {
                    iDBAccess.LinkGridControl(gcSubArea);
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 2)
                {
                    MessageBox.Show("该分区在数据库中已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnSubAreaAdd.Enabled = true;

        }

        //更新分区
        private void btnSubAreaUpdate_Click(object sender, EventArgs e)
        {
            iDBAccess = new Subarea();
            Cursor = Cursors.WaitCursor;
            btnSubAreaUpdate.Enabled = false;
            if (SubAreaDataCheck())
            {

                GridSubArea_Info subAreaInfo = new GridSubArea_Info();
                subAreaInfo.SubAreaID = _subAreaInfo.SubAreaID;
                subAreaInfo.HeightNum = Convert.ToInt32(txtHeightNum.Text);
                subAreaInfo.IsUseful = chkSubAreaIsUse.Checked;
                subAreaInfo.NameHeadChar = txtHeardTitle.Text;
                subAreaInfo.OutTimeAlarmTime = Convert.ToInt32(txtOutTimeAlarm.Text);
                subAreaInfo.SubAreaName = txtSubAreaName.Text;
                subAreaInfo.WidthNum = Convert.ToInt32(txtWidthNum.Text);

                int ret = iDBAccess.Update(subAreaInfo);
                if (ret == -1)
                {
                    MessageBox.Show("更新失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 0)
                {
                    iDBAccess.LinkGridControl(gcSubArea);
                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("该分区在数据库中已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnSubAreaUpdate.Enabled = true;
        }
        //删除分区
        private void btnSubAreaDelete_Click(object sender, EventArgs e)
        {

            iDBAccess = new Subarea();
            Cursor = Cursors.WaitCursor;
            btnSubAreaDelete.Enabled = false;
            if (SubAreaDataCheck())
            {


                int ret = iDBAccess.Delete(_subAreaInfo);
                if (ret == -1)
                {
                    MessageBox.Show("删除失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 0)
                {
                    iDBAccess.LinkGridControl(gcSubArea);
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnSubAreaDelete.Enabled = true;
        }
        /// <summary>
        /// 分区数据检查
        /// </summary>
        /// <returns></returns>
        private bool SubAreaDataCheck()
        {
            if (IsTextNull(txtSubAreaName, "分区名称"))
            {
                return false;
            }
            if (IsTextNull(txtWidthNum, "横向格子数"))
            {
                return false;
            }
            if (IsTextNull(txtSubAreaName, "分区名称"))
            {
                return false;
            }
            if (IsTextNull(txtHeightNum, "纵向格子数"))
            {
                return false;
            }
            if (IsTextNull(txtHeardTitle, "头字母"))
            {
                return false;
            }
            if (IsTextNull(txtOutTimeAlarm, "超时报警时间"))
            {
                return false;
            }

            return true;
        }
        #endregion

        private void btncali_Click(object sender, EventArgs e)
        {
            //ClassVectorPointClass vec = new ClassVectorPointClass();
            //string a = vec.CalibrationWirzard();
        }

        private void simpleButtonMakepicOk_Click(object sender, EventArgs e)
        {
            IniFile.FilePath = Application.StartupPath;
            IniFile.IniWriteValue("Host", "FontSize", spinEdit1.Value.ToString());
            IniFile.IniWriteValue("Host", "BarCodeSize", barCodeSize.Value.ToString());
            IniFile.IniWriteValue("Host", "BarCodeFontIsBold", checkBox2.Checked ? "True" : "False");
            IniFile.IniWriteValue("Host", "AddIccByPs", comboBoxICC.SelectedIndex.ToString());
            IniFile.IniWriteValue("Host", "CodeHeight", spinEditCodeHeight.Value.ToString());
            IniFile.IniWriteValue("Host", "IsPrintBoardInfo", checkBoxIsBoardInfo.Checked ? "True" : "False");
            IniFile.IniWriteValue("Host", "IsPrintLine", checkBoxIsprintLine.Checked ? "True" : "False");
            IniFile.IniWriteValue("Host", "IsAutoCut", checkBoxAutocut.Checked ? "True" : "False");
            IniFile.IniWriteValue("Host", "BarCodeType", comboBoxBarCodeType.SelectedIndex.ToString());
            IniFile.IniWriteValue("Host", "MinPhotoRemarkLength", textEditMinbarLength.Text);
            IniFile.IniWriteValue("Host", "BackColor", label10.BackColor.R + "," + label10.BackColor.G + "," + label10.BackColor.B);
            //IniFileOper.IniWriteValue("Host", "PrintRemarkOrder = textEditF1.Text + "," + textEditF2.Text + "," + textEditF3.Text;
            IniFile.IniWriteValue("Host", "CutMinPhotoNum", textEditCutMinPhotoNum.Text);
            IniFile.IniWriteValue("Host", "PhotoRemarkFlag", comboBoxPhotoRemarkFlag.SelectedIndex.ToString());
            IniFile.IniWriteValue("Host", "TextAlignType", comboBoxAlignType.SelectedIndex.ToString());//0左1右2居中
            IniFile.IniWriteValue("Host", "SaveFileType", comboBoxSaveColorType.SelectedIndex.ToString());//0RGB 1Cmyk
            IniFile.IniWriteValue("Host", "TextFontSize", spinEdit1.Text);
            IniFile.IniWriteValue("Host", "TextFont", comboBoxFont.Text.Trim());
            IniFile.IniWriteValue("Host", "CutInfoPosition", comboBoxCutInfoPosition.SelectedIndex.ToString());

            IniFile.IniWriteValue("Host", "RemarkInPicRightOrLeft", comboBoxRightOrLeft.SelectedIndex.ToString());
            IniFile.IniWriteValue("Host", "RemarkInPicBottomOrTop", comboBoxBottomOrTop.SelectedIndex.ToString());

            IniFile.IniWriteValue("Host", "HistoryDataSaveDays", txtDays.Text);

            IniFile.IniWriteValue("Host", "TwoCutFlag", checkBoxTwoFlag.Checked? "True" : "False");

           // IniFile.IniWriteValue("Host", "MerginMM", textEditMergenMM.Text);

            //DateTransfer.BoardLineWidth=HistoryDataSaveDays
            DateTransfer.CutMinPhotoNum = Convert.ToInt16(textEditCutMinPhotoNum.Text);
            DateTransfer.IsAutoCut = checkBoxAutocut.Checked;
            DateTransfer.IsPrintLine = checkBoxIsprintLine.Checked;
            DateTransfer.MinPhotoRemarkLength = Convert.ToInt16(textEditMinbarLength.Text);
            DateTransfer.PhotoRemarkFlag = comboBoxPhotoRemarkFlag.SelectedIndex;
            DateTransfer.SaveFileType = comboBoxSaveColorType.SelectedIndex;
            DateTransfer.SelectBarCodeType = comboBoxBarCodeType.SelectedIndex;
            DateTransfer.TextAlignType = comboBoxAlignType.SelectedIndex;
            DateTransfer.ColorBackStr = label10.BackColor.R + "," + label10.BackColor.G + "," + label10.BackColor.B;
            DateTransfer.AddIccByType = comboBoxICC.SelectedIndex;
            DateTransfer.TextFont = comboBoxFont.Text.Trim();
            DateTransfer.TextFontSize = Convert.ToInt32(spinEdit1.Value);
            DateTransfer.HistoryDataSaveDays = Convert.ToInt32(txtDays.Text);
            DateTransfer.BarCodeSize = Convert.ToInt32(barCodeSize.Value);
            DateTransfer.TwoCutFlag = checkBoxTwoFlag.Checked;
            //DateTransfer.MerginMM = Convert.ToInt32(textEditMergenMM.Text);
            //DateTransfer.CutNobarCode = checkBoxCutNobarCode.Checked;
            //if (comboBoxBleed.SelectedIndex == 0)
            //{
            //    int bLInt = comboBoxBleed.SelectedIndex;
            //    Double preserntDo = Convert.ToDouble(textEditMergenMM.Text);
            //    Bleed sBleed = new Bleed(bLInt, preserntDo);
            //    DateTransfer.CurrentBleed = sBleed;
            //}
            //else
            //{
            //    int bLInt = comboBoxBleed.SelectedIndex;
            //    Double preserntDo = Convert.ToDouble(textEditMergenMM.Text);
            //    Bleed sBleed = new Bleed(bLInt, preserntDo);
            //    DateTransfer.CurrentBleed = sBleed;
            //}
            int bLInt = comboBoxBleed.SelectedIndex;
            Double preserntDo = Convert.ToDouble(textEditMergenMM.Text);
            Bleed sBleed = new Bleed(bLInt, preserntDo);
            DateTransfer.CurrentBleed = sBleed;
            SaveBarSerToSetting();
            try
            {
               
                Save();
            }
            catch (Exception)
            {

                throw;
            }
            MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Save()
        {
            if (string.IsNullOrEmpty(txtDays.Text))
            {
                txtDays.Focus();
                MessageBox.Show("请输入天数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            IniFile.FilePath = Application.StartupPath;
            IniFile.IniWriteValue("Host", "HistoryDataSaveDays", txtDays.Text);
           
            //MessageBox.Show("设置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //this.Close();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void simpleButtonT1_Click(object sender, EventArgs e)
        {
            var result = WNetConnectionHelper.WNetAddConnection(textEditU1.Text, textEditP1.Text, txtHotFilePath.Text, null);

            if (result == 0)
            {
                MessageBox.Show("测试成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("无法访问请先设置打印机的热文件夹", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            var result = WNetConnectionHelper.WNetAddConnection(textEditU.Text, textEditP.Text, txtBigPicPath.Text, null);

            if (result == 0)
            {
                MessageBox.Show("测试成功");
            }
            else
            {
                MessageBox.Show("无法访问请先设置打印机的热文件夹");
            }
        }

        private void labelControl31_Click(object sender, EventArgs e)
        {

        }

        private void xtraTabControl_Click(object sender, EventArgs e)
        {

        }

        private void comboBoxBarCodeType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void simpleButtonSelectColor_Click(object sender, EventArgs e)
        {
            //colorDialog1.AllowFullOpen = true;
            //colorDialog1.FullOpen = true;
            //colorDialog1.ShowHelp = true;
            //colorDialog1.Color = Color.Black;
            //if (DialogResult.OK == colorDialog1.ShowDialog())
            //{
            //    label10.BackColor = colorDialog1.Color;
            //}
        }

        private void cmbPrinterPaperType_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            PrinterSet printerSet = new PrinterSet();
            PrinterSeting_info printerSetInfo = printerSet.GetPrinterIccPath(_printerInfo.PrinterID, cmbPrinterPaperType.Text);
            if (printerSetInfo != null)
            {
                txtSharpValue.Text = printerSetInfo.Sharpening.ToString();
                txtICCFilePath.Text = printerSetInfo.ICCPATH;
            }
            else
            {
                txtSharpValue.Text = string.Empty;
                txtICCFilePath.Text = string.Empty;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void textEditF1_EditValueChanged(object sender, EventArgs e)
        {
            int f1text = Convert.ToInt32(string.IsNullOrEmpty(textEditF1.Text) ? "0" : textEditF1.Text);
            int f2text = Convert.ToInt32(string.IsNullOrEmpty(textEditF2.Text) ? "0" : textEditF2.Text);
            int f3text = Convert.ToInt32(string.IsNullOrEmpty(textEditF3.Text) ? "0" : textEditF3.Text);
            if (f1text == 1)
            {
                if (f2text <= f3text)
                {
                    textEditF2.Text = "2";
                    textEditF3.Text = "3";
                }
                else
                {
                    textEditF2.Text = "3";
                    textEditF3.Text = "2";
                }
            }
            else if (f1text == 2)
            {
                if (f2text <= f3text)
                {
                    textEditF2.Text = "1";
                    textEditF3.Text = "3";
                }
                else
                {
                    textEditF2.Text = "3";
                    textEditF3.Text = "1";
                }
            }
            else
            {
                if (f2text <= f3text)
                {
                    textEditF2.Text = "1";
                    textEditF3.Text = "2";
                }
                else
                {
                    textEditF2.Text = "2";
                    textEditF3.Text = "1";
                }
            }
        }

        private void textEditF2_EditValueChanged(object sender, EventArgs e)
        {
            int f1text = Convert.ToInt32(string.IsNullOrEmpty(textEditF1.Text) ? "0" : textEditF1.Text);
            int f2text = Convert.ToInt32(string.IsNullOrEmpty(textEditF2.Text) ? "0" : textEditF2.Text);
            int f3text = Convert.ToInt32(string.IsNullOrEmpty(textEditF3.Text) ? "0" : textEditF3.Text);
            if (f2text == 1)
            {
                if (f1text <= f3text)
                {
                    textEditF1.Text = "2";
                    textEditF3.Text = "3";
                }
                else
                {
                    textEditF1.Text = "3";
                    textEditF3.Text = "2";
                }
            }
            else if (f2text == 2)
            {
                if (f1text <= f3text)
                {
                    textEditF1.Text = "1";
                    textEditF3.Text = "3";
                }
                else
                {
                    textEditF1.Text = "3";
                    textEditF3.Text = "1";
                }
            }
            else
            {
                if (f1text <= f3text)
                {
                    textEditF1.Text = "1";
                    textEditF3.Text = "2";
                }
                else
                {
                    textEditF1.Text = "2";
                    textEditF3.Text = "1";
                }
            }
        }

        private void textEditF3_EditValueChanged(object sender, EventArgs e)
        {
            int f1text = Convert.ToInt32(string.IsNullOrEmpty(textEditF1.Text) ? "0" : textEditF1.Text);
            int f2text = Convert.ToInt32(string.IsNullOrEmpty(textEditF2.Text) ? "0" : textEditF2.Text);
            int f3text = Convert.ToInt32(string.IsNullOrEmpty(textEditF3.Text) ? "0" : textEditF3.Text);
            if (f3text == 1)
            {
                if (f1text <= f2text)
                {
                    textEditF1.Text = "2";
                    textEditF2.Text = "3";
                }
                else
                {
                    textEditF1.Text = "3";
                    textEditF2.Text = "2";
                }
            }
            else if (f3text == 2)
            {
                if (f1text <= f2text)
                {
                    textEditF1.Text = "1";
                    textEditF2.Text = "3";
                }
                else
                {
                    textEditF1.Text = "3";
                    textEditF2.Text = "1";
                }
            }
            else
            {
                if (f1text <= f2text)
                {
                    textEditF1.Text = "1";
                    textEditF2.Text = "2";
                }
                else
                {
                    textEditF1.Text = "2";
                    textEditF2.Text = "1";
                }
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            //将选中的项提向上
            BarSerInfo selBarSet = gridView1.GetFocusedRow() as BarSerInfo;
            if (selBarSet != null)
            {
                if (selBarSet.BarIndex > 1)
                {
                    _listR.FirstOrDefault(s => s.BarIndex == selBarSet.BarIndex - 1).BarIndex = selBarSet.BarIndex;
                    selBarSet.BarIndex -= 1;
                    _listR = _listR.OrderBy(s => s.BarIndex).ToList();
                    gridControl1.DataSource = _listR;
                    gridControl1.RefreshDataSource();
                    gridView1.SelectRow(selBarSet.BarIndex - 1);
                    gridView1.FocusedRowHandle = selBarSet.BarIndex - 1;
                }
                else
                {
                    MessageBox.Show("已是最前面了，不能再向前!");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //将选中的项提向下
            BarSerInfo selBarSet = gridView1.GetFocusedRow() as BarSerInfo;
            if (selBarSet != null)
            {
                if (selBarSet.BarIndex < _listR.Count)
                {
                    _listR.FirstOrDefault(s => s.BarIndex == selBarSet.BarIndex + 1).BarIndex = selBarSet.BarIndex;
                    selBarSet.BarIndex += 1;
                    _listR = _listR.OrderBy(s => s.BarIndex).ToList();
                    gridControl1.DataSource = _listR;
                    gridControl1.RefreshDataSource();
                    gridView1.SelectRow(selBarSet.BarIndex - 1);
                    gridView1.FocusedRowHandle = selBarSet.BarIndex - 1;
                }
                else
                {
                    MessageBox.Show("已是最后了，不能再向后!");
                }
            }
        }

        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                BarSerInfo bs = gridView1.GetRow(e.RowHandle) as BarSerInfo;
                if (bs != null)
                {
                    e.Appearance.BackColor = bs.IsPrint ? Color.White : Color.Gray;
                }
            }
        }
        /// <summary>
        /// 根据用户的调整将条码顺序的信息写入到setting中
        /// </summary>
        private void SaveBarSerToSetting()
        {
            IniFile.FilePath = Application.StartupPath;
            List<BarSerInfo> listR = gridControl1.DataSource as List<BarSerInfo>;
            if (listR != null)
            {
                StringBuilder sbR = new StringBuilder();
                foreach (var barSet in listR)//
                {
                    sbR.Append(barSet.BarIndex + "_" + barSet.Code + "_" + barSet.Description + "_" + Convert.ToInt32(barSet.IsPrint) + ",");
                }
                sbR.Remove(sbR.Length - 1, 1);//去除最后一个","

                IniFile.IniWriteValue("Host", "BarSetting", sbR.ToString());
            }

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label10_Click(object sender, EventArgs e)
        {
            colorDialog1.AllowFullOpen = true;
            colorDialog1.FullOpen = true;
            colorDialog1.ShowHelp = true;
            colorDialog1.Color = Color.Black;
            if (DialogResult.OK == colorDialog1.ShowDialog())
            {
                label10.BackColor = colorDialog1.Color;
            }
        }

        private void gvPrinter_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            _printerInfo = (Printer_info)gvPrinter.GetFocusedRow();
            if (_printerInfo != null)
            {
                txtDPI.Text = _printerInfo.PDPI.ToString();
                txtHotFilePath.Text = _printerInfo.HotDirPath;
                txtMonitorFilePath.Text = _printerInfo.MonitorDirPath;
                txtPrinterDes.Text = _printerInfo.PDes;
                txtPrinterLength.Text = _printerInfo.PLength.ToString();
                txtPrinterName.Text = _printerInfo.PName;
                txtPrinterWidth.Text = _printerInfo.PWidth.ToString();
                chkIsHorizontalPrint.Checked = _printerInfo.IsTransverse;
                textEditU1.Text = _printerInfo.HotFolderUserName;
                textEditP1.Text = _printerInfo.HotFolderPwd;
                checkEditIsMiddle.Checked = _printerInfo.IsMiddle;
                comboBoxPrinterType.SelectedIndex = _printerInfo.PrinterType;
            }
            cmbPrinterPaperType.Text = "";
            txtSharpValue.Text = "";
            txtICCFilePath.Text = "";
        }

        private void checkBoxClear_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxClear.Checked)
            {
                txtOrderPath.Enabled = false;
            }
            else
            {
                txtOrderPath.Enabled = true;
            }

        }

        private void radioButtonDelive_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonDelive.Checked)
            {
                txtOrderPath.Enabled = true;
                btnOrderPathBrowser.Enabled = true;
            }
            else
            {
                txtOrderPath.Enabled = false;
            }
        }

        private void label10_MouseMove(object sender, MouseEventArgs e)
        {
            //this.Cursor = Cursors.Hand;
        }

        private void checkPrinterName_CheckedChanged(object sender, EventArgs e)
        {
            IniFile.FilePath = Application.StartupPath;
            if (checkPrinterName.Checked)
            {
                
                IniFile.IniWriteValue("Host", "BoolFilePrinterName", "true");
            }
            else
            {
                IniFile.IniWriteValue("Host", "BoolFilePrinterName", "false");
            }
        }

        private void checkSize_CheckedChanged(object sender, EventArgs e)
        {
            IniFile.FilePath = Application.StartupPath;
            if (checkSize.Checked)
            {
                IniFile.IniWriteValue("Host", "BoolTempleteName", "true");
            }
            else
            {
                IniFile.IniWriteValue("Host", "BoolTempleteName", "false");
            }
        }

        private void btnBigCutPathBrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog.ShowDialog())
            {
                string path = folderBrowserDialog.SelectedPath;
                ////if (!path.Contains(@"\\"))
                ////{
                //    MessageBox.Show("请选择网络路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                ////}
                textEditCutInfoPath.Text = path;
            }
        }

        private void checkBoxIsprintLine_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void comboBoxPhotosizeUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            IniFile.FilePath = Application.StartupPath;

            IniFile.IniWriteValue("Host", "PhotosizeUnit", comboBoxPhotosizeUnit.SelectedIndex.ToString());
            
        }

        private void labelLineColor_Click(object sender, EventArgs e)
        {
            colorDialog2.AllowFullOpen = true;
            colorDialog2.FullOpen = true;
            colorDialog2.ShowHelp = true;
            colorDialog2.Color = Color.Black;
            if (DialogResult.OK == colorDialog2.ShowDialog())
            {
                labelLineColor.BackColor = colorDialog2.Color;
                IniFile.IniWriteValue("Host", "LineBackColor", colorDialog2.Color.R + "," + colorDialog2.Color.G + "," + colorDialog2.Color.B);
            }
        }

        private void simpleButtonOtherOK_Click(object sender, EventArgs e)
        {
            string RemarkKeysColor = "";
            string key1 = (textBoxkey1.Text.Trim());
            if (!string.IsNullOrEmpty(key1))
            {
                Color c1=labelRemarkColor1.BackColor;
                RemarkKeysColor +=key1+"@"+c1.R + "," + c1.G + "," + c1.B+"#";

            }
            string key2 = (textBoxkey2.Text.Trim());
            if (!string.IsNullOrEmpty(key2))
            {
                Color c = labelRemarkColor2.BackColor;
                RemarkKeysColor += key2 + "@" + c.R + "," + c.G + "," + c.B + "#";

            }
            string key3 = (textBoxkey3.Text.Trim());
            if (!string.IsNullOrEmpty(key3))
            {
                Color c = labelRemarkColor3.BackColor;
                RemarkKeysColor += key3 + "@" + c.R + "," + c.G + "," + c.B + "#";

            }
            string key4 = (textBoxkey4.Text.Trim());
            if (!string.IsNullOrEmpty(key4))
            {
                Color c = labelRemarkColor4.BackColor;
                RemarkKeysColor += key4 + "@" + c.R + "," + c.G + "," + c.B + "#";

            }
            string key5 = (textBoxkey5.Text.Trim());
            if (!string.IsNullOrEmpty(key5))
            {
                Color c = labelRemarkColor5.BackColor;
                RemarkKeysColor += key5 + "@" + c.R + "," + c.G + "," + c.B + "#";

            }
            string key6 = (textBoxkey6.Text.Trim());
            if (!string.IsNullOrEmpty(key6))
            {
                Color c = labelRemarkColor6.BackColor;
                RemarkKeysColor += key6 + "@" + c.R + "," + c.G + "," + c.B + "#";

            }
            string key7 = (textBoxkey7.Text.Trim());
            if (!string.IsNullOrEmpty(key7))
            {
                Color c = labelRemarkColor7.BackColor;
                RemarkKeysColor += key7 + "@" + c.R + "," + c.G + "," + c.B + "#";

            }
            string key8 = (textBoxkey8.Text.Trim());
            if (!string.IsNullOrEmpty(key8))
            {
                Color c = labelRemarkColor8.BackColor;
                RemarkKeysColor += key8 + "@" + c.R + "," + c.G + "," + c.B + "#";

            }
            IniFile.IniWriteValue("Host", "RemarkKeysColor", RemarkKeysColor.Trim('#'));
            DateTransfer.RemarkKeysColor = RemarkKeysColor.Trim('#');
        }

        void SetColor(object sender)
        {
            colorDialog3.AllowFullOpen = true;
            colorDialog3.FullOpen = true;
            colorDialog3.ShowHelp = true;
            colorDialog3.Color = Color.Black;
            if (DialogResult.OK == colorDialog3.ShowDialog())
            {
                ((Label)sender).BackColor = colorDialog3.Color;
            }
 
        }

        private void labelRemarkColor1_Click(object sender, EventArgs e)
        {
            SetColor(sender);
        }

        private void labelRemarkColor2_Click(object sender, EventArgs e)
        {
            SetColor(sender);
        }

        private void labelRemarkColor3_Click(object sender, EventArgs e)
        {
            SetColor(sender);
        }

        private void labelRemarkColor4_Click(object sender, EventArgs e)
        {
            SetColor(sender);
        }

        private void labelRemarkColor5_Click(object sender, EventArgs e)
        {
            SetColor(sender);
        }

        private void labelRemarkColor6_Click(object sender, EventArgs e)
        {
            SetColor(sender);
        }

        private void labelRemarkColor7_Click(object sender, EventArgs e)
        {
            SetColor(sender);
        }

        private void labelRemarkColor8_Click(object sender, EventArgs e)
        {
            SetColor(sender);
        }

        private void comboBoxRightOrLeft_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBoxBleed_DropDownClosed(object sender, EventArgs e)
        {
            if (comboBoxBleed.SelectedIndex == 0)
            {
                labelBleedUnit.Text = "mm";
            }
            else
            {
                labelBleedUnit.Text = "%";
            }

        }




    }
    public class BarSerInfo
    {
        public int BarIndex { get; set; }//显示的顺序
        public string Code { get; set; }//代码
        public string Description { get; set; }
        public bool IsPrint { get; set; }//是否打印
    }

}
